<template>
  <div class="minh110p">

    <!--动态-->
    <div v-for="(v,k) in lists" :key="k">
      <p class="fs12 ca8 pl20 lh40">{{v[0].titleTime}}</p>
      <div class="pl16_prod">
        <ProdItem :prod="v2"
                  :type="'complex'"
                  :key="k2"
                  v-for="(v2,k2) in v"
                  @btn_tap="btn_tap"
                  @prod_tap="prod_tap"></ProdItem>
      </div>
    </div>

    <div v-if="addTypeShow">
      <SelectorOne
        :title="'选择添加内容'"
        :status="addTypeShow"
        :allClass="addTypes"
        @closeModal="addTypeShow = !addTypeShow"
        @choose_tap="choose_tap"></SelectorOne>
    </div>


    <BottomButtonSmall :text="'发布'" :url="'confirm'" @btn_tap="btn_tap"></BottomButtonSmall>

    <!--bottom-->
    <div class="textc lh42 fs12 ca8 bgf5f6" v-if="nodata">- 到底了，看看其他的吧 -</div>
  </div>
</template>

<script>
  import BottomButtonSmall from '@/components/bottom_button_small' // 订单项
  import ProdItem from '@/components/prodItem' // 订单项
  import SelectorOne from '@/components/selectorOne' // 订单项
  import WXAJAX from '@/utils/request'
  import util from '@/utils/index'

  export default {
    name: '',
    components: {BottomButtonSmall,ProdItem,SelectorOne},
    data() {
      return{
        lists:[],
        addTypeShow:false,
        addTypes:[
          {name:'添加文章' , id:'article'},
          {name:'添加动态' , id:'dynamic'},
        ],
        page:1,
        isLoading : false ,//是否在加载
        nodata:false ,//是否已经没有数据
      }
    },
    onShow(){
      this.nodata = false ;
      this.lists = [] ;
      this.page = 1 ;
      this.isLoading = false ;
      wx.setStorageSync('editOperator', '');
      this.dynamic();
    },
    mounted() {
      wx.setNavigationBarTitle({
        title: "动态管理"
      });
    },
    async onPullDownRefresh() {
      // to doing..
      // 下拉刷新
      wx.showNavigationBarLoading();
      this.nodata = false ;
      this.lists = [] ;
      this.page = 1 ;
      this.isLoading = false ;
      this.dynamic();
      wx.stopPullDownRefresh();
      setTimeout(function () {
        wx.hideNavigationBarLoading();
      },300)
    },
    onReachBottom() {
      let v = this ;
      v.dynamic();
    },
    methods: {
      btn_tap(method){
        if(method == 'confirm'){//发布
          this.addTypeShow = true ;
        }else if(method == 'like'){//收藏
          console.log('收藏')
        }else if(method == 'comment'){//评论
          console.log('评论')
        }
      },
      prod_tap(id , type ){
        //type : 1 -- 文章 ， 2 -- 动态
        if( type ==1 ){
          wx.navigateTo({url:'../articleDetail/main?id='+id});
        }else{
          wx.navigateTo({url:'../dynamicDetail/main?id='+id});
        }
      },
      choose_tap(type){//发布类型
        this.addTypeShow = false;
        wx.navigateTo({url:'../dynamicIssue/main?type='+type});
      },
      dynamic(){
        let v = this ;
        if(v.isLoading){
          wx.hideLoading();
          return
        }
        v.isLoading = true ;
        wx.showLoading() ;
        WXAJAX.POST({
          pageSize : 4 ,
          pageNum : v.page ,
        },'','/company/selectDynamicInfo').then((data) => {
          wx.hideLoading();
          if(data){
            let today = new Date(),
              year = today.getFullYear(),
              month = today.getMonth(),
              day = today.getDate(),
              item = {},
              _datas = {},
              _length = v.lists.length ,
              _lastFullTime = 0 ;

            _length !=0 && (_lastFullTime = v.lists[_length-1][0].fullTime) ;

            data.forEach(function (i, k) {
              var time = new Date(i.createTime),
                fullTime = time.getFullYear() + '' + time.getMonth() + '' + time.getDate(),
                showTime = '',
                info = '';

              showTime = time.getFullYear() + '-' + time.getMonth() + '-' + time.getDate() ;

              item = {
                fullTime: fullTime,//年月日
                titleTime : showTime ,
                createTime : time.getHours() + ':' + time.getMinutes() ,
                photo : i.photos ? i.photos.split(',')[0] : [],
                title : i.title ,
                commentModelList : i.commentModelList ,
                doThumbsNum : i.doThumbsNum ,
                id : i.dynamicId ,
                type : i.type
              };

              if(_lastFullTime == fullTime){
                v.lists[_length-1].push(item);
              }else{
                if (_datas[fullTime]) {
                  _datas[fullTime].push(item);
                } else {
                  _datas[fullTime] = [];
                  _datas[fullTime].push(item);
                }
              }
            });

            var newkey = Object.keys(_datas).sort();
            //先用Object内置类的keys方法获取要排序对象的属性名，再利用Array原型上的sort方法对获取的属性名进行排序，newkey是一个数组
            var newObj = {};//创建一个新的对象，用于存放排好序的键值对
            for (var i = 0; i < newkey.length; i++) {//遍历newkey数组
              newObj[newkey[i]] = _datas[newkey[i]];//向新创建的对象中按照排好的顺序依次增加键值对
            }

            _datas = [];
            for (var i in newObj) {
              _datas.push(newObj[i]);
            }
            _datas = _datas.reverse();

            v.lists = [...v.lists , ..._datas ];
            v.page ++ ;
            setTimeout(function () {
              v.isLoading = false ;
            },500);
          }else{
            setTimeout(function () {
              v.isLoading = false ;
            },500);
          }
        }).catch((err) =>{
          wx.hideLoading();
          console.log(err)
          if(err.code == 204){
            v.nodata = true ;
          }
          setTimeout(function () {
            v.isLoading = false ;
          },500);
        });
      }
    }
  }
</script>

<style>
  .pl16_prod>div{
    padding-left: 20px;
  }
</style>



















